﻿using Design.Common.Utils;

namespace SingletonPattern
{
    internal sealed class LazybonesSingleton
    {
        private static volatile LazybonesSingleton _instance;
        private const string _name = "懒汉模式";
        private static readonly object _locker = new();

        private LazybonesSingleton()
        {
            ConsoleExpansion.WriteLine($"{_name} 创建实例:{Guid.NewGuid()}");
        }

        public static LazybonesSingleton Instance
        {
            get
            {
                ConsoleExpansion.WriteLine($"{_name} 获取实例 is null {_instance == null}");

                lock (_locker)
                {
                    _instance ??= new();
                }

                return _instance;
            }
        }
    }
}